// import { useStorage } from '@vueuse/core'
import artTemplate from "art-template"
import dot from "dot"
import {dayjs} from "element-plus"
import _ from "lodash"

/**
 * 状态值
 */
export const state =  reactive({
  data: `{
    "tableComment": "用户表",
    "tableName": "sys_user",
    "columns": [
      {
        "columnComment": "主键",
        "columnName": "id",
        "columnType": "varchar(64)",
        "isPrimary": true,
        "isAutoIncrement": false,
        "isNonNull": true,
        "columnDefault": null
      },
      {
        "columnComment": "账号",
        "columnName": "username",
        "columnType": "varchar(64)",
        "isPrimary": false,
        "isAutoIncrement": false,
        "isNonNull": true,
        "columnDefault": null
      },
      {
        "columnComment": "密码",
        "columnName": "password",
        "columnType": "varchar(256)",
        "isPrimary": false,
        "isAutoIncrement": false,
        "isNonNull": true,
        "columnDefault": null
      },
      {
        "columnComment": "姓名",
        "columnName": "name",
        "columnType": "varchar(64)",
        "isPrimary": false,
        "isAutoIncrement": false,
        "isNonNull": false,
        "columnDefault": null
      },
      {
        "columnComment": "手机号",
        "columnName": "phone",
        "columnType": "varchar(64)",
        "isPrimary": null,
        "isAutoIncrement": false,
        "isNonNull": false,
        "columnDefault": null
      },
      {
        "columnComment": "邮箱",
        "columnName": "email",
        "columnType": "varchar(64)",
        "isPrimary": null,
        "isAutoIncrement": false,
        "isNonNull": false,
        "columnDefault": null
      },
      {
        "columnComment": "登录错误次数",
        "columnName": "err_count",
        "columnType": "int",
        "isPrimary": false,
        "isAutoIncrement": false,
        "isNonNull": false,
        "columnDefault": null
      },
      {
        "columnComment": "身价资产(元)",
        "columnName": "personal_value",
        "columnType": "decimal(18,2)",
        "isPrimary": false,
        "isAutoIncrement": false,
        "isNonNull": false,
        "columnDefault": null
      },
      {
        "columnComment": "状态(0-正常 1-停用)",
        "columnName": "status",
        "columnType": "varchar(1)",
        "isPrimary": false,
        "isAutoIncrement": false,
        "isNonNull": true,
        "columnDefault": "'0'"
      },
      {
        "columnComment": "创建人",
        "columnName": "create_by",
        "columnType": "varchar(64)",
        "isPrimary": false,
        "isAutoIncrement": false,
        "isNonNull": false,
        "columnDefault": null
      },
      {
        "columnComment": "创建时间",
        "columnName": "create_at",
        "columnType": "timestamp",
        "isPrimary": false,
        "isAutoIncrement": false,
        "isNonNull": false,
        "columnDefault": null
      },
      {
        "columnComment": "修改人",
        "columnName": "update_by",
        "columnType": "varchar(64)",
        "isPrimary": false,
        "isAutoIncrement": false,
        "isNonNull": false,
        "columnDefault": null
      },
      {
        "columnComment": "修改时间",
        "columnName": "update_at",
        "columnType": "timestamp",
        "isPrimary": false,
        "isAutoIncrement": false,
        "isNonNull": false,
        "columnDefault": null
      },
      {
        "columnComment": "删除标志(0-未删除 1-已删除)",
        "columnName": "delete_flag",
        "columnType": "varchar(1)",
        "isPrimary": false,
        "isAutoIncrement": false,
        "isNonNull": true,
        "columnDefault": "'0'"
      }
    ]
  }`.trim(),
  tmpl: `
  import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fofund.ark.common.annotation.Keyword;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import lombok.Data;
import org.hibernate.validator.constraints.Length;

import java.util.List;
<%
/* 通用字段的特殊处理 */
const modelName = pascal(tableName.split('_')[1])  // 去掉sys_前缀
const createColumns = ['create_by','create_at']
const updateColumns = ['update_by','update_at'] 
const deleteFlag = 'delete_flag'
%>
/**
 * {{_.trimEnd(tableComment, '表')}}
 *
 * @author 何鹏举 {{dayjs().format('YYYY-MM-DD')}}
 */
@Data
@TableName("{{tableName}}") @Schema(description = "{{_.trimEnd(tableComment, '表')}}")
public class {{modelName}} {
   {{each columns c}}
    @Schema(description = "{{c.columnComment}}"){{if c.isPrimary}}
    @TableId(type = IdType.ASSIGN_ID){{/if}}{{if createColumns.indexOf(_.toLower(c.columnName)) > 0}}
    @TableField(fill = FieldFill.INSERT, updateStrategy = FieldStrategy.NEVER){{/if}}{{if updateColumns.indexOf(_.toLower(c.columnName)) > 0}}
    @TableField(fill = FieldFill.UPDATE, insertStrategy = FieldStrategy.NEVER){{/if}}{{if deleteFlag === c.columnName}}
    @TableLogic  @JsonIgnore{{/if}}
    private {{typeMapping(c.columnType, 'java')}} {{camel(c.columnName)}};
   {{/each}}
}
  `.trim()
})


/**
 * 多模板渲染
 */
export const engines = ['art', 'lodash', 'dot']
export function render(name, source, data) {
  try {
    switch (name) {
      case "art"     : return artTemplate.render(source, data, {imports: {dayjs, _, ...extFunctions}})
      case "lodash"  : return _.template(source, {imports: {dayjs, ...extFunctions}})(data)
      case "dot"     : return dot.template(source)({dayjs, _, ...extFunctions, ...data})
      default        : return `${name} 模板引擎暂不支持`
    }
  } catch (e) {
    return `${name}模板报错: ${e.message}`
  }
}

/**
 * 额外函数
 */
const extFunctions = {
  // 转驼峰
  camel: str => str?.replace(/_(\w)/g, (_, p1) => p1.toUpperCase()),

  // 帕斯卡命名（大驼峰）
  pascal: str => {
    const newStr = str?.replace(/_(\w)/g, (_, p1) => p1.toUpperCase())
    return newStr ? newStr.charAt(0).toUpperCase() + newStr.slice(1) : newStr;
  },

  // 下划线
  snake: str => str?.replace(/([A-Z])/g, '_$1').toLowerCase()
}
